<?php
class ArticleController extends ManagerController {
	function ArticleController(){
		parent::ManagerController();
	}
	
	function index(){
		$art_mod = new ArticleModel();
		self::init_page_info();
		$condition = array('site_id'=>SITE_ID);
		
		//获取多语言
		if(count(Lang::get_site_langs())>1){
			if(isset($_GET['lang'])){
				$lang = $_GET['lang'];
			}else{
				$lang = LANGUAGE;
			}
			$columns = $lang.'.title';
		}else{
			$lang = null;
			$columns = 'title';
		}
		$articles = $art_mod->get_articles($condition, $columns, $lang);
		self::set_page_total($art_mod->count($condition));
		$this->assign('articles', $articles);
		$this->assign('types', $this->get_recommend_types());
		$this->display();
	}
	
	public function sort(){
		$mod = new RecommendModel();
		$type = $_POST['type'];
		$ids = $_POST['ids'];
		$recommend = $mod->get_by_attrs(array('type'=>$type, 'model'=>'article', 'site_id'=>SITE_ID));
		$id_list = explode(';', $ids);
		$i = 0;
		foreach ($id_list as $id){
			if(empty($id))continue;
			$recommend['items'][$id]['rank'] = $i;
			++$i;
		}
		$mod->update_by_attrs(array('type'=>$type, 'model'=>'article', 'site_id'=>SITE_ID), $recommend);
		$this->ajax_info('保存成功！');
	}
	
	public function del_recommend(){
		$ids = $_POST['ids'];
		$type = $_POST['type'];
		$mod = new RecommendModel();
		foreach($ids as $id){
			if(empty($id))continue;
			$mod->delete_column(array('type'=>$type, 'model'=>'article', 'site_id'=>SITE_ID), 'items.' . $id);
		}
		$this->ajax_info('删除成功！');
	}
	
	private function get_recommend_types(){
		return array(
			'0' => '首页推荐'
		);
	}

	public function edit_recommend(){
		$mod = new RecommendModel();
		$rid = $_REQUEST['rid'];
		$recommend = $mod->get_by_key_site_limit($rid, 'type,items.' . $this->id);
		if(IS_POST){
			$title = self::get_post_val('title');
			$mod->update_by_key_site_limit($rid, array(
					'items.' . $this->id . '.title' => $title,
			));
			$this->redirect('/manager.php?app=article&act=recommend&type=' . $recommend['type']);
		}
		$article_mod = new ArticleModel();
		$article = $article_mod->get_by_key_site_limit($this->id, 'title');
		$this->assign('article', $article);
		$this->assign('recommend', $recommend['items'][$this->id]);
		$this->display();
	}

	public function add_recommend(){
		$type = $_POST['type'];
		$article_ids = $_POST['article_ids'];
		if(empty($article_ids)){
			$this->ajax_error('未选择有引用文章！');
		}
		$recommend_mod = new RecommendModel();
		$recommend = $recommend_mod->get_by_attrs(array('site_id'=>SITE_ID, 'type'=>$type, 'model'=>'article'));
		$is_new = false;
		if(empty($recommend)){
			$recommend = array(
				'site_id'=>SITE_ID, 
				'type'=>$type, 
				'model'=>'article',
				'_id' => gen_guid(),
			);
			$is_new = true;
		}
		$rank = 100;
		foreach ($article_ids as $article_id){
			$mod = new ArticleModel();
			$article = $mod->get_by_key_site_limit($article_id);
			$cur_rank = $rank;
			if(isset($recommend['items'][$article_id])){
				$cur_rank = $recommend['items'][$article_id]['rank'];
			}
			$recommend['items'][$article_id] = array(
				'title' => $article['title'],
				'cate' => $article['cate'],
				'rank' => $cur_rank,
			);
			$rank += 10;
		}
		if($is_new){
			$recommend_mod->insert($recommend);
		}else{
			$recommend_mod->update_by_key_site_limit($recommend['_id'], $recommend);
		}
		$this->ajax_info('保存成功！');
	}
	
	public function edit(){
		if(IS_POST){
			$h = new FormHelper(array('name'=>'article_form', 'add_site_id'=>true, 'post_add_time'=>true));
			$result = $h->save($result);
			$this->ajax_info($result);
		}
		$this->display();
	}
	
	private function save(){
		$title = self::get_post_val('title');
		$lang = self::get_post_val('lang');
		$cate = $_POST['cate'];
		$summary = self::get_post_val('summary');
		$html = self::get_post_val('html');
		$template = self::get_post_val('template');
		$tag = self::get_post_val('tag');
		$from = self::get_post_val('from');
		$read = self::get_post_int_val('read', 0);
		$content = array();
		for($i=0;$i<100;++$i){
			if(!isset($_POST['content_'.$i])){
				break;
			}
			$body = $_POST['content_'.$i];
			if(empty($body)){
				continue;
			}
			$content[] = $body;
		}
		$seo_title = self::get_post_val('seo_title');
		$seo_description = self::get_post_val('seo_description');
		$seo_keywords = self::get_post_val('seo_keywords');
		
		if($title==null || utf8_strlen($title)<1){
			self::ajax_error('标题名称长度应该在1至50之间！');
		}
		if(!Lang::is_lang($lang)){
			self::ajax_error('非法语言名称！');
		}
		if(isset($_POST['use_my_name'])){
			$author = $this->user_info['user_name'];
		}else{
			$author = self::get_post_val('author');
		}
		
		$article = array(
			'site_id' 	=> SITE_ID,							//网站ID
			'author' 	=> $author,							//作者
			'user_id' 	=> $this->user_info['_id'],			//作者ID
			'html'		=> $html,							//页面名称，不带.html
			'template'	=> $template,						//指定模板，不带.htm
			'from'		=> $from,							//文章来源
			'read'		=> $read,							//阅读次数
			$lang		=> array(
				'title' 			=> $title,				//文章标题
				'summary' 			=> $summary,			//文章概要
				'content' 			=> $content,			//文章内容
				'seo_title' 		=> $seo_title,			//SEO标题
				'seo_description' 	=> $seo_description,	//SEO描述
				'seo_keywords' 		=> $seo_keywords,		//SEO关键字
			), 
		);
		//分类
		if(!empty($cate)){
			$cates = array();
			foreach($cate as $c){
				$cates[$c] = 10000;
			}
			$article['cate'] = $cates;
		}
		//标签
		if(!empty($tag)){
			$tag = preg_replace('/(　|;|,)/i', ' ', $tag);
			$tags = explode(' ', $tag);
			$article[$lang]['tags'] = $tags;
		}
		
		$article_mod = new ArticleModel();
		if(!empty($this->id)){
			//修改
			$old_art = $article_mod->get_by_attrs(array('_id'=>$this->id, 'site_id'=>SITE_ID));
			if(empty($old_art)){
				self::ajax_error('要修改的文章不存在或者已被删除！');
			}
			if(isset($old_art['cate'])){
				unset($old_art['cate']);
			}
			if(isset($old_art[$lang]['content'])){
				unset($old_art[$lang]['content']);
			}
			if(isset($old_art[$lang]['tags'])){
				unset($old_art[$lang]['tags']);
			}
			//合并
			$article = array_merge($old_art, $article);
			$article_mod->delete_by_key($this->id);
		}else{
			//新增
			$article['_id'] = gen_guid();				//文章ID
			$article['create_time'] = time();			//创建时间
			$article['collect'] = 0;					//被收藏次数
			$article['comment'] = 0;					//评论次数
		}
		$article_mod->insert($article);
		self::ajax_info('文章保存成功！');
	}
	
	public function get_attrs(){
		$ids = $_POST['ids'];
		$attr_helper = new AttrHelper();
		$attr_config = $attr_helper->get_attr_configs('article', $ids, $this->id);

		$this->assign('attr_configs', $attr_config);
		$this->display();
	}

	public function recommend(){
		$mod = new RecommendModel();
		if(isset($_REQUEST['type'])){
			$type = $_REQUEST['type'];
		}else{
			$type = 0;
		}
		$recommend = $mod->get_by_attrs(array('type'=>$type, 'model'=>'article', 'site_id'=>SITE_ID));
		if(!empty($recommend)){
			$recommend['items'] = SortHelper::sortByCol($recommend['items'], 'rank');
			$this->assign('recommend', $recommend);
		}
		$this->assign('types', $this->get_recommend_types());
		$this->display();
	}

}

?>